package com.battle.service.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.QuestionBankDetails;
import com.battle.pojo.entity.QuestionBankDetailsStudy;
import com.battle.pojo.param.exam.CreateRandomQuestionParam;
import com.battle.pojo.param.questionBankDetailsStudy.*;
import com.battle.pojo.vo.exam.AppointQuestionJsonVo;
import com.battle.pojo.vo.exam.GetExamVo;
import com.battle.pojo.vo.questionBankDetailsStudy.*;
import com.battle.pojo.dto.questionBankDetailsStudy.*;
import com.battle.service.repository.in.ExamRepository;
import com.battle.service.repository.in.QuestionBankDetailsRepository;
import com.battle.service.repository.in.QuestionBankDetailsStudyRepository;
import com.battle.service.repository.in.QuestionBankRepository;
import com.battle.service.service.in.ExamService;
import com.battle.service.service.in.QuestionBankDetailsStudyService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：题目学习服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class QuestionBankDetailsStudyServiceImpl implements QuestionBankDetailsStudyService {

    private final QuestionBankDetailsRepository detailsRepository;
    private final QuestionBankDetailsStudyRepository studyRepository;
    private final QuestionBankRepository bankRepository;
    private final ExamService examService;

    public QuestionBankDetailsStudyServiceImpl(QuestionBankDetailsRepository detailsRepository, QuestionBankDetailsStudyRepository studyRepository, QuestionBankRepository bankRepository, ExamService examService) {
        this.detailsRepository = detailsRepository;
        this.studyRepository = studyRepository;
        this.bankRepository = bankRepository;
        this.examService = examService;
    }


    /**
     * 创建题目学习
     *
     * @param param
     */
    @Override
    public void createQuestionBankDetailsStudy(CreateQuestionBankDetailsStudyParam param) {
        QuestionBankDetailsStudy questionBankDetailsStudy = new QuestionBankDetailsStudy();
        BeanUtils.copyProperties(param, questionBankDetailsStudy);
        Long id = IdGenerator.SNOW.generate();
        questionBankDetailsStudy.setId(id);

        //根据题目id查询
        QuestionBankDetails details = detailsRepository.getByIdCheck(param.getQuestionBankDetailsId());
        BeanUtils.copyProperties(details, questionBankDetailsStudy);
        questionBankDetailsStudy.setQuestionId(details.getId());

        String userAnswer = JSONArray.toJSONString(param.getUserAnswer());
        questionBankDetailsStudy.setUserAnswer(userAnswer);
        //两边答案是否匹配
        Boolean equalsAnswer = equalsAnswer(param.getUserAnswer(), details.getAnswer());
        questionBankDetailsStudy.setIsCorrect(equalsAnswer);

//        //如果是考试，是需要计算得分得
//        if (param.getExamId() != null && param.getExamType() == 1) {
//            //根据考试id查询
//            GetExamVo exam = examService.getExam(param.getExamId());
//            //如果是随机题目
//            if (exam.getIsRandomQuestion()) {
//                List<CreateRandomQuestionParam> randomQuestionJson = exam.getRandomQuestionJson();
//                //根据题目类型获取分数
//                for (CreateRandomQuestionParam createRandomQuestionParam : randomQuestionJson) {
//                    if (createRandomQuestionParam.getType().equals(details.getType())) {
//                        questionBankDetailsStudy.setScore(createRandomQuestionParam.getScore());
//                    }
//                }
//            } else {
//                //如果是指定题目
//                List<AppointQuestionJsonVo> questionJsonVos = exam.getAppointQuestionVoList();
//                //根据题目id获取分数
//                for (AppointQuestionJsonVo appointQuestionJsonVo : questionJsonVos) {
//                    if (appointQuestionJsonVo.getQuestionId().equals(param.getQuestionBankDetailsId())) {
//                        questionBankDetailsStudy.setScore(appointQuestionJsonVo.getScore());
//                    }
//                }
//            }
//        }

        studyRepository.create(questionBankDetailsStudy);
    }


    Boolean equalsAnswer(List<String> userAnswer, String answer) {
        //1.将两边答案转换成数组
        List<String> answerList = JSONArray.parseArray(answer, String.class);
        //2.将两边答案排序
        userAnswer.sort(String::compareTo);
        answerList.sort(String::compareTo);
        //3.比较两边答案是否一致
        return userAnswer.equals(answerList);
    }


    public static void main(String[] args) {
        String userAnswer = "[\"A\",\"B\"]";
        String answer = "[\"B\",\"A\"]";
        //1.将两边答案转换成数组
        List<String> strings = JSONArray.parseArray(userAnswer, String.class);
        List<String> strings1 = JSONArray.parseArray(answer, String.class);
        //2.将两边答案排序
        strings.sort(String::compareTo);
        strings1.sort(String::compareTo);
        //3.比较两边答案是否一致
        boolean equals = strings.equals(strings1);
        //4.如果一致则正确，否则错误
        System.out.println(equals);
        if (equals) {
            System.out.println("正确");
        } else {
            System.out.println("错误");
        }

    }

    /**
     * 修改题目学习
     *
     * @param param
     */
    @Override
    public void editQuestionBankDetailsStudy(EditQuestionBankDetailsStudyParam param) {
        Long id = param.getId();
        QuestionBankDetailsStudy questionBankDetailsStudy = studyRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, questionBankDetailsStudy);
        studyRepository.edit(questionBankDetailsStudy);
    }

    /**
     * 移除题目学习
     *
     * @param id
     */
    @Override
    public void removeQuestionBankDetailsStudy(Long id) {
        studyRepository.removeById(id);
    }

    /**
     * 启动/禁用题目学习
     *
     * @param id
     */
    @Override
    public void enabledQuestionBankDetailsStudy(Long id) {
        QuestionBankDetailsStudy questionBankDetailsStudy = studyRepository.getByIdCheck(id);
        questionBankDetailsStudy.setIsDisable(!questionBankDetailsStudy.getIsDisable());
        studyRepository.edit(questionBankDetailsStudy);
    }

    /**
     * 获取题目学习详情
     *
     * @param id
     * @return
     */
    @Override
    public GetQuestionBankDetailsStudyVo getQuestionBankDetailsStudy(Long id) {
        QuestionBankDetailsStudy questionBankDetailsStudy = studyRepository.getByIdCheck(id);
        GetQuestionBankDetailsStudyVo vo = new GetQuestionBankDetailsStudyVo();
        BeanUtils.copyProperties(questionBankDetailsStudy, vo);
        vo.setBankStudyId(questionBankDetailsStudy.getQuestionBankStudyId());
        return vo;
    }

    /**
     * 获取题目学习 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetQuestionBankDetailsStudyListVo> getQuestionBankDetailsStudyList(GetQuestionBankDetailsStudyListParam param) {

        SelectQuestionBankDetailsStudyDto dto = new SelectQuestionBankDetailsStudyDto();
        BeanUtils.copyProperties(param, dto);
        Page<QuestionBankDetailsStudy> page = studyRepository.selectQuestionBankDetailsStudy(dto);
        List<QuestionBankDetailsStudy> questionBankDetailsStudyList = page.getRecords();
        List<GetQuestionBankDetailsStudyListVo> list = new ArrayList<>();
        for (QuestionBankDetailsStudy questionBankDetailsStudy : questionBankDetailsStudyList) {
            GetQuestionBankDetailsStudyListVo vo = new GetQuestionBankDetailsStudyListVo();
            BeanUtils.copyProperties(questionBankDetailsStudy, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取题目学习下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetQuestionBankDetailsStudyDownListVo> getQuestionBankDetailsStudyDownList(GetQuestionBankDetailsStudyDownListParam param) {
        QuestionBankDetailsStudy po = new QuestionBankDetailsStudy();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<QuestionBankDetailsStudy> questionBankDetailsStudyList = studyRepository.getListByEntity(po);

        List<GetQuestionBankDetailsStudyDownListVo> list = new ArrayList<>();
        for (QuestionBankDetailsStudy questionBankDetailsStudy : questionBankDetailsStudyList) {
            GetQuestionBankDetailsStudyDownListVo vo = new GetQuestionBankDetailsStudyDownListVo();
            BeanUtils.copyProperties(questionBankDetailsStudy, vo);
            list.add(vo);
        }
        return list;
    }


}



